﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
//using System.Collections;
using System.Xml;
using System.Web.Services;

public partial class Finance_ProcClaer : ATPages
{
    Function f = new Function();
    ZSql zs = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            yjTm1.Text = Request.QueryString["yjTm1"];
            yjTm2.Text = Request.QueryString["yjTm2"];
            SpecificationsName.Text = Request.QueryString["SpecificationsName"];
            BrandName.Text = Request.QueryString["BrandName"];
            ComoditiesName.Text = Request.QueryString["ComoditiesName"];
            ComodBarcode.Text = Request.QueryString["ComodBarcode"];
            TypeStocck.SelectedValue = Request.QueryString["TypeStocck"];
            Warehouse.SelectedValue = Request.QueryString["Warehouse"];
            initGvData();//邦定数据 
            ddlComodName();
        }
    }
    private void initGvData()//初使化数据
    {
        string search = "";
        string countSql = "";
        string sql = "SELECT * FROM (SELECT stockId,ComodBarcode, ProcNum,typeStock,typeStockId,WarehouseId,ProcMoney,SuppliMoney, CONVERT(char(10), storageTime, 120) AS storageTime, EditUserName, AuditState,(select isnull(sum(number),0) from CS_Sales where ComodBarcode=a.comodBarcode and WarehouseId=a.WarehouseId) as saleNum, (SELECT WarehouseName FROM CS_Warehouse   WHERE WarehouseId = a.WarehouseId) AS WarehouseName, (SELECT Supplier FROM CS_Supplier WHERE SupplierId = a.SupplierId) AS SupplierName, (SELECT Brand  FROM Brand WHERE BrandId = a.BrandId) AS BrandName, (SELECT ComoditiesName FROM ComoditiesName  WHERE ComoditiesNameId = a.ComoditiesName) AS ComoditiesName, (SELECT Specifications FROM Specifications    WHERE SpecificationsId = a.ModelId) AS SpecificationsName, (SELECT Units FROM Units  WHERE UnitsId = a.UnitId) AS UnitsName FROM CS_Stock a ) Stock where 1=1 ";
        if (!string.IsNullOrEmpty(Request.QueryString["yjTm1"]) && !string.IsNullOrEmpty(Request.QueryString["yjTm2"]))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
            sql += " and storageTime>='" + Request.QueryString["yjTm1"] + "' and storageTime<='" + Request.QueryString["yjTm2"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["SpecificationsName"]))
        {
            search += "&SpecificationsName=" + Common.FormatParameter(SpecificationsName.Text.Trim());
            sql += " and SpecificationsName like'%" + Request.QueryString["SpecificationsName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["BrandName"]))
        {
            search += "&BrandName=" + Common.FormatParameter(BrandName.Text.Trim());
            sql += " and BrandName like'%" + Request.QueryString["BrandName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComoditiesName"]))
        {
            search += "&ComoditiesName=" + Common.FormatParameter(ComoditiesName.Text.Trim());
            sql += " and ComoditiesName like'%" + Request.QueryString["ComoditiesName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
            sql += " and ComodBarcode like'%" + Request.QueryString["ComodBarcode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["TypeStocck"]))
        {
            search += "&TypeStocck=" + Common.FormatParameter(TypeStocck.SelectedValue.Trim());
            sql += " and typeStockId =" + Request.QueryString["TypeStocck"];

        }
        if (!string.IsNullOrEmpty(Request.QueryString["Warehouse"]))
        {
            search += "&Warehouse=" + Common.FormatParameter(Warehouse.SelectedValue.Trim());
            sql += " and WarehouseId ='" + Request.QueryString["Warehouse"] + "' ";
        }
        //统计countSql：countProcNum，CountSuppliMoney，CountSuppliMoney
        countSql = "select isnull(sum(ProcNum),0) as countProcNum,isnull(sum(ProcMoney*ProcNum),0) as countProcMoney,isnull(sum(SuppliMoney*ProcNum),0) as CountSuppliMoney from(" + sql + ") DERIVEDTBL";
        sql += " ORDER BY stockId DESC";

        PageNavigator1.Url = "../Finance/ProcClaer.aspx?moduid=" + hfxs.Value + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

        //Response.Write(countSql);
        //Response.End();
        //读取统计数据
        zs.Open(countSql);
        if (zs.m_table.Rows.Count > 0)
        {
            CountProcNum.Text = zs.m_table.Rows[0]["countProcNum"].ToString();
            CountProcMoney.Text = zs.m_table.Rows[0]["countProcMoney"].ToString();
            CountSuppliMoney.Text = zs.m_table.Rows[0]["CountSuppliMoney"].ToString();
        }
        zs.Close();


    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
        }
        if (!string.IsNullOrEmpty(SpecificationsName.Text.Trim()))
        {
            search += "&SpecificationsName=" + Common.FormatParameter(SpecificationsName.Text.Trim());
        }
        if (!string.IsNullOrEmpty(BrandName.Text.Trim()))
        {
            search += "&BrandName=" + BrandName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComoditiesName.Text.Trim()))
        {
            search += "&ComoditiesName=" + ComoditiesName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComodBarcode.Text))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(TypeStocck.SelectedValue.Trim()))
        {
            search += "&TypeStocck=" + Common.FormatParameter(TypeStocck.SelectedValue.Trim());
        }
        if (!string.IsNullOrEmpty(Warehouse.SelectedValue.Trim()))
        {
            search += "&Warehouse=" + Common.FormatParameter(Warehouse.SelectedValue.Trim());
        }
        //Response.Write(TypeStocck.SelectedValue);
        //Response.End();
        PageNavigator1.Url = "../Finance/ProcClaer.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("ProcClaer.aspx?moduid=" + hfxs.Value + search);
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)//权限设置
    {
    }

    protected void ddlComodName()
    {
            ZSql ware = new ZSql();
            ware.Open("select warehouseid,warehousename from CS_Warehouse ");
            if (ware.m_table.Rows.Count > 0)
            {
                Warehouse.DataSource = ware.m_table;
                Warehouse.DataTextField = "warehousename";
                Warehouse.DataValueField = "warehouseid";
                Warehouse.DataBind();
                Warehouse.Items.Insert(0, new ListItem("所有仓库", ""));

                ware.Close();
            }

    }
    protected void Excel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=ProcClaer" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvData.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }
}
